package com.example.registerdiirectionlayoutyd203zbq2021606145.slice;

import com.example.registerdiirectionlayoutyd203zbq2021606145.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

public class MainAbilitySlice extends AbilitySlice {
    TextField TF1,TF2;
    Button BTN,BTN2;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        TF1 = (TextField) findComponentById(ResourceTable.Id_tf1);
        TF2 = (TextField) findComponentById(ResourceTable.Id_tf2);
        BTN = (Button) findComponentById(ResourceTable.Id_btn);
        BTN2 = (Button) findComponentById(ResourceTable.Id_btn2);

        BTN.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                String inputTF1 = TF1.getText();
                String inputTF2 = TF2.getText();
                if (inputTF1.equals("")){
                    new ToastDialog(getContext()).setText("用户名不能为空").show();
                }else {
                    if (inputTF2.equals("")){
                        new ToastDialog(getContext()).setText("密码不能为空").show();
                    }else {
                        DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
                        Preferences preferences = databaseHelper.getPreferences("data");
                        preferences.putString("name", inputTF1);
                        preferences.putString("pwd", inputTF2);
                        preferences.flush();
                        new ToastDialog(getContext()).setText("注册成功").show();
                    }
                }

            }
        });
        BTN2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                String inputTF1 = TF1.getText();
                String inputTF2 = TF2.getText();
                if (inputTF1.equals("")){
                    new ToastDialog(getContext()).setText("用户名不能为空").show();
                }else {
                    if (inputTF2.equals("")){
                        new ToastDialog(getContext()).setText("密码不能为空").show();
                    }else {
                        DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
                        Preferences preferences = databaseHelper.getPreferences("data");
                        String REAL_NAME = preferences.getString("name", "");
                        String REAL_PWD = preferences.getString("pwd", "");
                        if (inputTF1.equals(REAL_NAME) && inputTF2.equals(REAL_PWD)){
                            new ToastDialog(getContext()).setText("登录成功").show();
                        }else {
                            new ToastDialog(getContext()).setText("登录失败").show();

                        }
                    }
                }




            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
